﻿using System;
using System.Data;
using System.Globalization;
using System.Web.UI;
using mojoPortal.Web.Framework;
using mojoPortal.Web.UI;
using mojoPortal.Business;
using mojoPortal.Business.WebHelpers;
using mojoPortal.Web;
using ud_Article.Business;
using Resources;
using System.Collections;

namespace ud_Article.UI
{
    public partial class BlogDraftsPage : mojoBasePage
    {
        #region Properties

        private int moduleId = -1;
        private int pageId = -1;
        protected Double timeOffset;
        protected TimeZoneInfo timeZone;
        private string blogDateTimeFormat;
        ArticleConfiguration config = new ArticleConfiguration();

        protected string BlogDateTimeFormat
        {
            get { return blogDateTimeFormat; }
        }

        protected int PageId
        {
            get { return pageId; }
        }

        protected int ModuleId
        {
            get { return moduleId; }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Request.IsAuthenticated)
            {
                SiteUtils.RedirectToLoginPage(this);
                return;
            }

            SecurityHelper.DisableBrowserCache();

            LoadSettings();

            if (!WebUser.HasEditPermissions(siteSettings.SiteId, moduleId, CurrentPage.PageId))
            {
                SiteUtils.RedirectToEditAccessDeniedPage();
            }

            PopulateLabels();
            PopulateControls();

        }

        private void PopulateControls()
        {
            using (IDataReader reader = Article.GetDrafts(moduleId))
            {
                rptDrafts.DataSource = reader;
                rptDrafts.DataBind();
            }
        }

        protected string FormatBlogDate(DateTime startDate)
        {
            return timeZone != null ? TimeZoneInfo.ConvertTimeFromUtc(startDate, timeZone).ToString(blogDateTimeFormat) : startDate.AddHours(timeOffset).ToString(blogDateTimeFormat);
        }
        
        private void PopulateLabels()
        {
            Title = SiteUtils.FormatPageTitle(siteSettings, ArticleResources.BlogDraftsLink);
            litHeader.Text = ArticleResources.BlogDraftsHeading;

            blogDateTimeFormat = CultureInfo.CurrentCulture.DateTimeFormat.FullDateTimePattern;

            if (Master != null)
            {
                Control c = Master.FindControl("Breadcrumbs");
                if (c != null)
                {
                    BreadcrumbsControl crumbs = (BreadcrumbsControl)c;
                    crumbs.ForceShowBreadcrumbs = true;
                }
            }
        }

        private void LoadSettings()
        {
            timeOffset = SiteUtils.GetUserTimeOffset();
            timeZone = SiteUtils.GetUserTimeZone();
        }
        #region OnInit

        override protected void OnInit(EventArgs e)
        {
            base.OnInit(e);
            Load += Page_Load;
            LoadParams();
            LoadPanels();

        }

        private void LoadParams()
        {
            pageId = WebUtils.ParseInt32FromQueryString("pageid", -1);
            moduleId = WebUtils.ParseInt32FromQueryString("mid", -1);
            Hashtable settings = ModuleSettings.GetModuleSettings(moduleId);
            config = new ArticleConfiguration(settings);
        }

        private void LoadPanels()
        {
            bool showLeftColumnAdmin = ConfigHelper.GetBoolProperty("ShowLeftColumnAdmin", false);
            bool showRightColumnAdmin = ConfigHelper.GetBoolProperty("ShowRightColumnAdmin", false);
            bool showTopColumnAdmin = ConfigHelper.GetBoolProperty("ShowTopColumnAdmin", false);
            bool showBottomColumnAdmin = ConfigHelper.GetBoolProperty("ShowBottomColumnAdmin", false);
            int showCenterColumnAdmin = ConfigHelper.GetIntProperty("ShowCenterColumnAdmin", 0);
            LoadSideContent(showLeftColumnAdmin, showRightColumnAdmin, showTopColumnAdmin, showBottomColumnAdmin, showCenterColumnAdmin);
        }
        #endregion
    }
}
